package com.hanrui.android.fragment.message.myvote;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.model.IPickerViewData;
import com.blankj.utilcode.util.ToastUtils;
import com.hanrui.android.R;
import com.hanrui.android.activity.download.MyDownLoadActivity;
import com.hanrui.android.library.base.BaseFragment;
import com.hanrui.android.model.MyVoteProposalBean;
import com.hanrui.android.net.contract.message.myvote.MyVoteHandleContract;
import com.hanrui.android.net.presenter.message.myvote.MyVoteHandlePresenter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 议案表决
 *
 */

public class MyVoteHandlelFragment extends BaseFragment<MyVoteHandlePresenter> implements MyVoteHandleContract.View {
    @BindView(R.id.ll_addview)
    LinearLayout lladdview;
    @BindView(R.id.login_out)
    TextView loginOut;

    String proposalList = "";

    String mid,type;

    JSONArray jsonArray;

    List<MyVoteProposalBean.ProposalListBean> list;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_myvotehandle;
    }

    @Override
    protected void initInjector() { mFragmentComponent.inject(this); }

    @Override
    protected void initView(View view) {

        mid = getArguments().getString("mid");
        type = getArguments().getString("type");

        mPresenter.proposalDetail(getActivity(),mid,type);

    }



    public static MyVoteHandlelFragment newInstance(String mid, String type) {
        MyVoteHandlelFragment fragment = new MyVoteHandlelFragment();
        Bundle bundle = new Bundle();
        bundle.putString("mid", mid);
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @OnClick({R.id.login_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_out:
                doPost();
                break;
        }
    }

    private void doPost() {


        if (jsonArray == null) {

            jsonArray = new JSONArray();
        }

        for (int i = 0; i < lladdview.getChildCount(); i++) {

            View childAt = lladdview.getChildAt(i);
            TextView vote = childAt.findViewById(R.id.bm);
            TextView remark = childAt.findViewById(R.id.vote_remark);

            String str = vote.getText().toString();

            if (TextUtils.isEmpty(str)) {

                ToastUtils.showShort("请选择表决");
                return;

            }
            String result;
            if (str.equals("同意")) {
                result = "0";
            }else if (str.equals("反对")) {
                result = "1";
            }else {
                result = "2";
            }

            JSONObject tmpObj = new JSONObject();

            try {
                tmpObj.put("id",list.get(i).getId());
                tmpObj.put("vote",result);
                tmpObj.put("remarks",remark.getText().toString());
                jsonArray.put(tmpObj);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


        proposalList = jsonArray.toString();
        mPresenter.saveProposalVote(getActivity(),mid,proposalList);
        jsonArray = null;

    }

    @Override
    public void proposalDetail(MyVoteProposalBean data) {


        if (null != data && null != data.getProposalList() && data.getProposalList().size() > 0) {

            list = data.getProposalList();

            for (int i = 0; i < list.size(); i++) {

                addViewItem(list.get(i));
            }

        }

    }

    @Override
    public void saveProposalVote(int code) {
        if (code == 1) {

            ToastUtils.showShort("提交成功");
            getActivity().finish();

        		}else {

            ToastUtils.showShort("提交失败");
        }

    }


    //添加ViewItem
    private void addViewItem(final MyVoteProposalBean.ProposalListBean bean) {

        View hotelEvaluateView = View.inflate(getActivity(), R.layout.item_myvotehandle, null);
        TextView billName = hotelEvaluateView.findViewById(R.id.bill_name);
        TextView billCompany = hotelEvaluateView.findViewById(R.id.bill_company);
        TextView billUser = hotelEvaluateView.findViewById(R.id.bill_user);
        LinearLayout bmLl = hotelEvaluateView.findViewById(R.id.bm_ll);
        final TextView bm = hotelEvaluateView.findViewById(R.id.bm);
        EditText voteRemark = hotelEvaluateView.findViewById(R.id.vote_remark);
        LinearLayout enclosureLl = hotelEvaluateView.findViewById(R.id.enclosure_ll);
        ImageView enclosure = hotelEvaluateView.findViewById(R.id.enclosure);

        billName.setText(bean.getProposal());
        billCompany.setText(bean.getShowOfficeName());
        billUser.setText(bean.getName());

        final List<VoteBean> voteList = getVoteDataList();

        final OptionsPickerView pvOptions = new OptionsPickerView.Builder(getActivity(), new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {

                bm.setText(voteList.get(options1).getPickerViewText());

            }
        }).build();
        pvOptions.setPicker(voteList);

        bmLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                pvOptions.show();

            }
        });

        if (null != bean.getFilesList() && bean.getFilesList().size() > 0) {

            enclosureLl.setVisibility(View.VISIBLE);
            enclosure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    MyDownLoadActivity.start(getActivity(),bean.getFilesList());

                }
            });

        }



        lladdview.addView(hotelEvaluateView);
    }


    private class VoteBean implements IPickerViewData {

        String name;

        public VoteBean(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String getPickerViewText() {
            return name;
        }

    }


    private List<VoteBean> getVoteDataList(){

        List<VoteBean> datList = new ArrayList<>();


        datList.add(new VoteBean("同意"));
        datList.add(new VoteBean("反对"));
        datList.add(new VoteBean("弃权"));

        return  datList;

    }

}
